package com.blog.attach.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blog.attach.entity.UserComment;
import com.blog.attach.entity.UserReplay;
import com.blog.attach.entity.VO.UserCommentVO;
import com.blog.attach.mapper.UserCommentMapper;
import com.blog.attach.mapper.UserReplayMapper;
import com.blog.attach.service.UserCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 苏离
 * @since 2022-01-18
 */
@SuppressWarnings("all")
@Slf4j
@Service
@Transactional
public class UserCommentServiceImpl extends ServiceImpl<UserCommentMapper, UserComment> implements UserCommentService {
@Autowired
    UserCommentMapper commentMapper;
@Autowired
    UserReplayMapper replayMapper;
    @Override
    public Map<String, Object> getComments(Integer articleId, Integer pageNo, Integer limit) {
        Integer commentsCount = commentMapper.getCommentsCount(articleId);
        if(commentsCount<1)
            return null;
        pageNo=(pageNo-1)*limit;
        List<UserCommentVO> commentsByArticle =
                commentMapper.getCommentsByArticle(articleId, pageNo, limit);
        Map map=new HashMap();
        map.put("count", commentsCount);
        map.put("comments", commentsByArticle);
        return map;
    }

    @Override
    public boolean insertComment(String userId, Integer articleId, String content) {
        UserComment comment=new UserComment();
        comment.setCommentUserId(userId).setCommentArticleId(articleId).setCommentContent(content);
        int insert = commentMapper.insert(comment);
        if(insert>0)
            return true;
            return false;
    }

    @Override
    public boolean deleteComment(Integer commentId) {
        QueryWrapper<UserComment> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("comment_id", commentId);
        int delete = commentMapper.delete(queryWrapper1);
        QueryWrapper<UserReplay> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("parent_id", commentId);
        int delete1 = replayMapper.delete(queryWrapper2);
        if(delete>0)
            return true;
        return false;
    }
}
